package com.situ.ymall.controller.api;

import com.situ.ymall.entity.APIResult;
import com.situ.ymall.entity.Shopcart;
import com.situ.ymall.entity.User;
import com.situ.ymall.service.ShopCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/api/cart")
public class APICartController {
    HashMap<Character, Integer> map = new HashMap<>();
    @Autowired
    private ShopCartService shopCartService;

    @GetMapping("/getAll")
    public List getAll(HttpSession session){
        //参数
        User user = (User) session.getAttribute("user");
        //调用service
        List list = shopCartService.getByUserId(user.getId());
        return list;
    }

    @GetMapping("/update")
    public APIResult update(Shopcart shopcart){
        Shopcart selectCart = shopCartService.update(shopcart);
        if (selectCart.getCount() == shopcart.getCount()){
            return APIResult.success(null,selectCart);
        }else {
            return APIResult.fail("库存不足，修改失败",selectCart);
        }
    }

    @GetMapping("/delete")
    public APIResult delete(Integer id){
        shopCartService.delete(id);
        return APIResult.success("成功",null);
    }
}
